<?php 
namespace app\superadmin\controller;
use app\superadmin\model\Index as IndexModel;
//继承Base控制器
class Shopadmin extends Base
{
    public function adminlist()
    {
    	if(request()->isPost()){
    		$data=[
    		  'adminname'=>input('adminname'),
    		  'adminpass'=>think_weiphp_md5(input('adminpass')),
    		  'sta'=>'0',
    		  'phone'=>input('phone'),
    		];
            //echo $data['adminpass'];die;
    		//添加数据前应该先验证
    		$validate = \think\Loader::validate('Shopadmins');

    		if($validate->check($data)){
    		$db=\think\Db::name('shopadmins')->insert($data);
    		if($db){
    			return $this->success('添加成功','Shopadmin/adminlist');
    		}
    		else{
    			return $this->error('添加失败','Shopadmin/adminlist');
    		 }
    		}else{
    			$this->error($validate->getError());
    		}
    		return;  
    	} 
       // $stateres= \think\Db::name('dining')
       //       ->alias('a')
       //       ->join('__SHOPADMINS__ b', 'a.admin_id =b.id')
       //       ->field('a.state')
       //       ->select();
       // $stateres= \think\Db::name('dining')
       //       ->field('admin_id')
       //       ->where('state=3')
       //       ->select();
       //  var_dump( $stateres);die;
        // if(in_array("3",$stateres))
        // {
          
        // }

        //获取餐厅管理员列表并进行分页
        //$shopadminsres= \think\Db::name('shopadmins')->paginate(3);

        $shopadminsres= \think\Db::name('shopadmins')
             ->alias('a')
             ->join('__DINING__ b', 'a.id =b.admin_id')
             ->field('a.id,a.adminname,a.phone,a.sta,b.shopname,b.state')
             ->where('a.id =b.admin_id')
             ->paginate(3);
//        $shopadminsres = \think\Db::view('shopadmins','adminname,id,states,phone')
//            ->view('dining','admin_id,shopname,state','shopadmins.id=admin_id')
//            ->select()
//            ->paginate(3);




        $this->assign('shopadminsres',$shopadminsres);
        //echo \think\Session::get('id');检测session是否写入成功
    	return $this->fetch();
    }


    public function resetpass(){
      $id=input('id');//获取id
      // $db=\think\Db::name('shopadmins')->where('id',$id)->value('adminpass');
      //echo $db;die;
      if($db=\think\Db::name('shopadmins')->where('id',$id)->update(['adminpass' =>md5('1234567')])){
    	 return $this->success('密码重置成功！','index');
      }else{
    	 return $this->error('密码已经初始化，重置失败！','Shopadmin/adminlist');
      }
       return;
    } 	
    

     public function changestate(){
      $id=input('id');//获取id
      //$state=input('state');//获取state
      //echo $state;die;
      if($db=\think\Db::name('shopadmins')->where('id',$id)->update(['sta' =>input('sta')])){
         //return $this->success('账号状态重置成功！','index');
         return $this->redirect('adminlist');
      }else{
         return $this->error('账号状态重置失败！','Shopadmin/adminlist');
      }
       return;
    }   
    

    public function del(){
    	$id=input('id');
    	if(db('shopadmins')->delete($id)){
    		return $this->success('删除成功！','Shopadmin/adminlist');
        //return $this->redirect('index');
    	}else{
    		return $this->error('删除成功！','Shopadmin/adminlist');
    	}
    }


}
